import { message } from 'antd';
import {
  allocationList,
  allotList,
  getUserList,
  unAllotList,
  receiveEntryList,
  getProgress,
  getSuppList,
  getListEs,
  getListCs,
  getListCustinfo,
  addAllotRule,
  modifyAllotRule,
  deleteRule,
} from '@/services/careermange';

import tableListModelFactory from '@/HOC/TableList/models/tableList';
import modelExtend from 'dva-model-extend';

const namespace = 'allocation';
const AllocationModel = {
  namespace,
  state: {
    visible: false,
    tab: {
      name: 'asgnState',
      use: true,
      activeKey: '2',
      list: [],
    },
    userList: [],
    esList: [],
    csList: [],
    custList: [],
    progressPercent: 0,
    suppList: [],
    autoModalVisible: false,
    detailModalVisible: false,
    csFlag: false,
    customeFlag: false,
    isDetail: false, // 是否点击查询
    serviceLabel: '员工服务人员',
    csLabel: '服务客服',
    customerLabel: '服务客户',
    actionType: 1, // 1:新增 2:修改 3:查看
    modifyObj: {},
    ruleTip:
      '说明:系统会自动将维护的客服或者客户对应的增员和减员任务自动分配;不同服务人员不能重复配置客服或客户',
  },

  effects: {
    *getSuppList({ payload }, { call, put }) {
      const response = yield call(getSuppList, payload);
      yield put({
        type: 'updateModelData',
        payload: {
          suppList: [],
        },
      });
      if (response.success && response.data) {
        yield put({
          type: 'updateModelData',
          payload: {
            suppList: Array.isArray(response.data) ? response.data : [],
          },
        });
      } else {
        message.warning(response.message || '获取签约方数据：服务端错误');
      }
    },
    // 获取列表
    *fetchList({ payload }, { call, put }) {
      const response = yield call(allocationList, payload); // 请求
      const {
        pagerArg: { currentPage, pageSize },
      } = payload;
      if (response.success) {
        yield put({
          type: 'getList',
          payload: {
            data: response.data,
            currentPage,
            pageSize,
          },
        });
      }
    },
    // 获取登录人
    *userList({ payload }, { call, put }) {
      const response = yield call(getUserList, payload);
      if (response.success) {
        yield put({
          type: 'getUserList',
          payload: response.data,
        });
      } else {
        message.warning(response.message);
      }
    },
    // 分配
    *allot({ payload }, { call }) {
      const response = yield call(allotList, payload);
      if (!response.success) {
        message.warning(response.message);
      }
      return response;
    },
    // 领取
    *receiveEntry({ payload }, { call }) {
      const response = yield call(receiveEntryList, payload);
      if (!response.success) {
        message.warning(response.message);
      }
      return response;
    },

    // 取消分配
    *unAllot({ payload }, { call }) {
      const response = yield call(unAllotList, payload);
      if (!response.success) {
        message.warning(response.message);
      }
      return response;
    },

    // 获取进度条
    *fetchProgress({ payload }, { call, put }) {
      const response = yield call(getProgress, payload);
      if (response.success) {
        yield put({
          type: 'updateModelData',
          payload: { progressPercent: response.data.successProp },
        });
      } else {
        message.warning(response.message);
      }
      return response;
    },

    *listEs({ payload }, { call, put }) {
      const response = yield call(getListEs, payload); // 请求
      if (response.success) {
        yield put({
          type: 'getListEs',
          payload: response.data,
        });
      } else {
        message.warning(response.message);
      }
    },
    *listCs({ payload }, { call, put }) {
      const response = yield call(getListCs, payload); // 请求
      if (response.success) {
        yield put({
          type: 'getListCs',
          payload: response.data,
        });
      } else {
        message.warning(response.message);
      }
    },

    *listCustinfo({ payload }, { call, put }) {
      const response = yield call(getListCustinfo, payload); // 请求
      if (response.success) {
        yield put({
          type: 'getListCustinfo',
          payload: response.data,
        });
      } else {
        message.warning(response.message);
      }
    },
    *addAllotRule({ payload }, { call }) {
      const response = yield call(addAllotRule, payload); // 请求
      if (response.success) {
        return response;
      }
      return message.warning(response.message);
    },
    *modifyAllotRule({ payload }, { call }) {
      const response = yield call(modifyAllotRule, payload); // 请求
      if (response.success) {
        return response;
      }
      return message.warning(response.message);
    },
    *deleteRule({ payload }, { call }) {
      const response = yield call(deleteRule, payload); // 请求
      if (!response.success) {
        message.warning(response.message);
      }
      return response;
    },
  },

  reducers: {
    // 获取列表
    getList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        data: {
          list,
          pagination: { total: data.totalCount, current: currentPage, pageSize },
        },
      };
    },
    // 获取登录人
    getUserList(state, { payload }) {
      return {
        ...state,
        userList: Array.isArray(payload) ? payload : [],
      };
    },
    //
    getListEs(state, { payload }) {
      return {
        ...state,
        esList: Array.isArray(payload) ? payload : [],
      };
    },
    getListCs(state, { payload }) {
      return {
        ...state,
        csList: Array.isArray(payload) ? payload : [],
      };
    },
    getListCustinfo(state, { payload }) {
      return {
        ...state,
        custList: Array.isArray(payload) ? payload : [],
      };
    },
    // 设置修改当前state数据
    updateModelData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    updateDetailModalVisible(
      state,
      {
        payload: { visible },
      }
    ) {
      return {
        ...state,
        detailModalVisible: visible,
      };
    },
    clearAutoFlag(state) {
      return {
        ...state,
        csFlag: undefined,
        customeFlag: undefined,
      };
    },
  },
};

export default modelExtend(
  tableListModelFactory({ namespace, fetchTLService: allocationList }),
  AllocationModel
);
